package com.manage.util;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.manage.base.BaseActivity;
import com.manage.http.AsyncHttpGet;
import com.manage.http.AsyncHttpPost;
import com.manage.http.BaseRequest;
import com.manage.http.DefaultThreadPool;
import com.manage.http.RequestException;
import com.manage.http.RequestParameter;
import com.manage.http.RequestResultCallback;
import com.manage.model.Shop;

public class RequestUtils {
	
	/**
	 * 获取今日账目and过去30天账目,按日期统计
	 * @param paramString1 token
	 * @param paramString2 shopid
	 * @param paramString3 startdate 开始日期
	 * @param paramString4 enddate 结束日期
	 * @param url
	 * @param handler
	 * @param requestList
	 */
	public static void getData(final String paramString1, final String paramString2,  
							   final String paramString3, final String paramString4,
							   final String url, final Handler handler, 
							   final List<BaseRequest> requestList) {
		Log.i("getData:", ""+paramString1+";"+paramString2);
		
		List<RequestParameter> parameterList = new ArrayList<RequestParameter>();
		parameterList.add(new RequestParameter("token", paramString1));
		parameterList.add(new RequestParameter("shopid", paramString2));
		parameterList.add(new RequestParameter("startdate", paramString3));
		parameterList.add(new RequestParameter("enddate", paramString4));
		
		AsyncHttpPost httpost = new AsyncHttpPost(null, url, parameterList,
			new RequestResultCallback(){

				@Override
				public void onSuccess(Object o) {
					Log.i("HttpPostExample", "HttpPostExample  request  onSuccess result :"+((String)o).toString());
						
					Message msg = new Message();
					msg.what = BaseActivity.SUCCESS;
					Bundle bundle = new Bundle();
					bundle.putString("res", ((String)o).toString());
					    
					msg.setData(bundle);
					handler.sendMessage(msg);
				}
					
				@Override
				public void onFail(Exception e) {
					Log.i("HttpPostExample", "HttpPostExample  request   onFail :"+((RequestException)e).getMessage());
						
					Message msg = new Message(); 
			        msg.what = BaseActivity.FAIL;
			        handler.sendMessage(msg);  //向Handler发送消息
				}
			
		});
		
		DefaultThreadPool.getInstance().execute(httpost);
		requestList.add(httpost);
	}
	
	/**
	 * 获取订单数据
	 * @param paramString1 商店id
	 * @param paramString2 用户访问令牌
	 * @param paramString3 1:待受理订单，2：待配送订单，3：待入账订单，4：已完成,5:已取消
	 * @param page
	 * @param handler
	 * @param requestList
	 */
	public static void getOrderData(final String paramString1, final String paramString2, 
								 final String paramString3, final String page, final Handler handler, 
								 final List<BaseRequest> requestList) {
		Log.i("getOrderData:", ""+paramString1+";"+paramString2+";"+paramString3);
		
		List<RequestParameter> parameterList = new ArrayList<RequestParameter>();
		parameterList.add(new RequestParameter("shopid", paramString1));
		parameterList.add(new RequestParameter("token", paramString2));
		parameterList.add(new RequestParameter("status", paramString3));
		parameterList.add(new RequestParameter("page", page));
		
		AsyncHttpPost httpost = new AsyncHttpPost(null, BaseActivity.ORDERSURL, parameterList,
			new RequestResultCallback(){

				@Override
				public void onSuccess(Object o) {
					Log.i("HttpPostExample", "HttpPostExample  request  onSuccess result :"+((String)o).toString());
						
					Message msg = new Message();
					msg.what = BaseActivity.SUCCESS;
					Bundle bundle = new Bundle();
					bundle.putString("res", ((String)o).toString());
					    
					msg.setData(bundle);
					handler.sendMessage(msg);
				}
					
				@Override
				public void onFail(Exception e) {
					Log.i("HttpPostExample", "HttpPostExample  request   onFail :"+((RequestException)e).getMessage());
						
					Message msg = new Message(); 
			        msg.what = BaseActivity.FAIL;
			        handler.sendMessage(msg);  //向Handler发送消息
				}
		});
		
		DefaultThreadPool.getInstance().execute(httpost);
		requestList.add(httpost);
	}
	
	/**
	 * 获取商店info，登录后的商店信息
	 * @param name
	 * @param pass
	 * @param handler
	 * @param requestList
	 */
	public static void getShopInfo(final String name, final String pass, 
							 final Handler handler, final List<BaseRequest> requestList, final Context paramContext) {
		
		List<RequestParameter> parameterList = new ArrayList<RequestParameter>();
		parameterList.add(new RequestParameter("shopname", name));
		parameterList.add(new RequestParameter("pass", pass));
		
		AsyncHttpPost httpost = new AsyncHttpPost(null, BaseActivity.LOGINURL, parameterList,
				new RequestResultCallback() {

				@Override
				public void onSuccess(Object o) {
					Log.i("HttpPostExample", "HttpPostExample  request  onSuccess result :"+((String)o).toString());
						
					try {
						JSONObject jsonTemp = new JSONObject(((String)o).toString());
						String return_info_fail = jsonTemp.get("signin_return").toString();
						boolean returnResult = Boolean.valueOf(return_info_fail);
							
						if(returnResult == false) {
							handler.sendEmptyMessage(BaseActivity.GETSHOPINFOFAIL);  //向Handler发送消息
						} else {
							Shop shop = new Shop();
							shop = Shop.setJson(((String)o).toString(), paramContext);
							
							Message msg = new Message();
							msg.what = BaseActivity.GETSHOPINFOSUCCESS;
							
						    Bundle mBundle = new Bundle();     
						    mBundle.putSerializable(BaseActivity.SER_KEY, shop);
						    
						    msg.setData(mBundle);
						    
						    handler.sendMessage(msg);
						}
							
					} catch (JSONException e) {
						e.printStackTrace();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
					
				@Override
				public void onFail(Exception e) {
					Log.i("HttpPostExample", "HttpPostExample  request   onFail :"+((RequestException)e).getMessage());
					
			        handler.sendEmptyMessage(BaseActivity.GETSHOPINFOFAIL);  //向Handler发送消息
				}
		});
		
		DefaultThreadPool.getInstance().execute(httpost);
		requestList.add(httpost);
	}
	
	/**
	 * 
	 * 修改订单状态
	 * @param paramString1 orderid
	 * @param paramString2 用户访问令牌
	 * @param paramString3 1:待受理订单，2：待配送订单，3：待入账订单，4：已完成,5:已取消
	 * @param paramString4 实收金额 
	 * @param paramString5 用户备注
	 * @param handler
	 * @param requestList
	 */
	public static void modifyOrder(final String paramString1, final String paramString2,
								   final String paramString3, final String paramString4, 
								   final String paramString5, final Handler handler, 
								   final List<BaseRequest> requestList) {
		
		List<RequestParameter> parameterList = new ArrayList<RequestParameter>();
		parameterList.add(new RequestParameter("orderid", paramString1));
		parameterList.add(new RequestParameter("token", paramString2));
		parameterList.add(new RequestParameter("status", paramString3));
		parameterList.add(new RequestParameter("paidup", paramString4));
		parameterList.add(new RequestParameter("remark", paramString5));
		
		AsyncHttpPost httpost = new AsyncHttpPost(null, BaseActivity.MODIFYORDERURL, parameterList,
				new RequestResultCallback(){

				@Override
				public void onSuccess(Object o) {
					Log.i("HttpPostExample", "HttpPostExample  request  onSuccess result :"+((String)o).toString());
						
					try {
						JSONObject jsonTemp = new JSONObject(((String)o).toString());
						boolean res = jsonTemp.getBoolean("status_return");
						
						if(res == true) {
							handler.sendEmptyMessage(BaseActivity.MODIFYORDERSUCCESS);
						} 
						else{
							handler.sendEmptyMessage(BaseActivity.MODIFYORDERFAIL);  
						} 
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
					
				@Override
				public void onFail(Exception e) {
					Log.i("HttpPostExample", "HttpPostExample  request   onFail :"+((RequestException)e).getMessage());
						
			        handler.sendEmptyMessage(BaseActivity.MODIFYORDERFAIL);  //向Handler发送消息
				}
		});
		
		DefaultThreadPool.getInstance().execute(httpost);
		requestList.add(httpost);
	}

	/**
	 * 
	 * @param paramString1 用户访问令牌
	 * @param paramString2 商店id
	 * @param paramString3 菜品分类
	 * @param url 		   url
	 * @param page         页码
	 * @param handler
	 * @param requestList
	 */
	public static void getOrderMenu(final String paramString1, final String paramString2, 
			 final String paramString3, final String url, 
			 final String page, final Handler handler, 
			 final List<BaseRequest> requestList) {
		List<RequestParameter> parameterList = new ArrayList<RequestParameter>();
		parameterList.add(new RequestParameter("token", paramString1));
		parameterList.add(new RequestParameter("shopid", paramString2));
		parameterList.add(new RequestParameter("sort", paramString3));
		parameterList.add(new RequestParameter("page", page));
		
		AsyncHttpPost httpost = new AsyncHttpPost(null, url, parameterList,
			new RequestResultCallback(){

				@Override
				public void onSuccess(Object o) {
					Log.i("HttpPostExample", "HttpPostExample  request  onSuccess result :"+((String)o).toString());
						
					Message msg = new Message();
					msg.what = BaseActivity.SUCCESS;
					Bundle bundle = new Bundle();
					bundle.putString("res", ((String)o).toString());
					    
					msg.setData(bundle);
					handler.sendMessage(msg);
				}
					
				@Override
				public void onFail(Exception e) {
					Log.i("HttpPostExample", "HttpPostExample  request   onFail :"+((RequestException)e).getMessage());
						
					Message msg = new Message(); 
			        msg.what = BaseActivity.FAIL;
			        handler.sendMessage(msg);  //向Handler发送消息
				}
		});
		
		DefaultThreadPool.getInstance().execute(httpost);
		requestList.add(httpost);
	}
	
	/**
	 * 获取评论
	 * @param paramString1  用户访问令牌
	 * @param paramString2  商店id
	 * @param paramString3  0:全部评价;1:15-13分；2：12-8分；3：8分以下
	 * @param url
	 * @param page
	 * @param handler
	 * @param requestList
	 */
	public static void getEvaluation(final String paramString1, final String paramString2, 
									 final String paramString3, final String page, 
									 final String url, final Handler handler, 
									 final List<BaseRequest> requestList) {
		List<RequestParameter> parameterList = new ArrayList<RequestParameter>();
		parameterList.add(new RequestParameter("token", paramString1));
		parameterList.add(new RequestParameter("shopid", paramString2));
		parameterList.add(new RequestParameter("segment", paramString3));
		parameterList.add(new RequestParameter("page", page));
		
//		AsyncHttpGet httpget = new AsyncHttpGet(null, url, parameterList,
//			new RequestResultCallback(){
//
//				@Override
//				public void onSuccess(Object o) {
//					Log.i("HttpGetExample", "HttpGetExample  request  onSuccess result :"+((String)o).toString());
//					
//					Message msg = new Message();
//					msg.what = BaseActivity.SUCCESS;
//					Bundle bundle = new Bundle();
//					bundle.putString("res", ((String)o).toString());
//					    
//					msg.setData(bundle);
//					handler.sendMessage(msg);
//				}
//					
//				@Override
//				public void onFail(Exception e) {
//					Log.i("HttpGetExample", "HttpGetExample  request   onFail :"+((RequestException)e).getMessage());
//					
//					Message msg = new Message(); 
//			        msg.what = BaseActivity.FAIL;
//			        handler.sendMessage(msg);  //向Handler发送消息
//				}
//			
//		});
		
		AsyncHttpPost httpost = new AsyncHttpPost(null, url, parameterList,
			new RequestResultCallback(){

				@Override
				public void onSuccess(Object o) {
					Log.i("HttpPostExample", "HttpPostExample  request  onSuccess result :"+((String)o).toString());
						
					Message msg = new Message();
					msg.what = BaseActivity.SUCCESS;
					Bundle bundle = new Bundle();
					bundle.putString("res", ((String)o).toString());
					    
					msg.setData(bundle);
					handler.sendMessage(msg);
				}
					
				@Override
				public void onFail(Exception e) {
					Log.i("HttpPostExample", "HttpPostExample  request   onFail :"+((RequestException)e).getMessage());
						
					Message msg = new Message(); 
			        msg.what = BaseActivity.FAIL;
			        handler.sendMessage(msg);  //向Handler发送消息
				}
		});
		
		DefaultThreadPool.getInstance().execute(httpost);
		requestList.add(httpost);
	}
	
	/**
	 * 
	 * @param paramString1 用户访问令牌
	 * @param paramString2 商店id
	 * @param paramString3 商店营业状态 1: 暂停营业，2：恢复营业
	 * @param handler
	 * @param requestList
	 */
	public static void suspendBusiness(final String paramString1, final String paramString2, 
			 final String paramString3, final Handler handler, 
			 final List<BaseRequest> requestList) {
		List<RequestParameter> parameterList = new ArrayList<RequestParameter>();
		parameterList.add(new RequestParameter("token", paramString1));
		parameterList.add(new RequestParameter("shopid", paramString2));
		parameterList.add(new RequestParameter("operate", paramString3));
		
		AsyncHttpPost httpost = new AsyncHttpPost(null, BaseActivity.SUSPENDBUSINESS, parameterList,
			new RequestResultCallback(){

				@Override
				public void onSuccess(Object o) {
					Log.i("HttpPostExample", "HttpPostExample  request  onSuccess result :"+((String)o).toString());
						
					Message msg = new Message();
					msg.what = BaseActivity.SUCCESS;
					Bundle bundle = new Bundle();
					bundle.putString("res", ((String)o).toString());
					    
					msg.setData(bundle);
					handler.sendMessage(msg);
				}
					
				@Override
				public void onFail(Exception e) {
					Log.i("HttpPostExample", "HttpPostExample  request   onFail :"+((RequestException)e).getMessage());
						
					Message msg = new Message(); 
			        msg.what = BaseActivity.FAIL;
			        handler.sendMessage(msg);  //向Handler发送消息
				}
		});
		
		DefaultThreadPool.getInstance().execute(httpost);
		requestList.add(httpost);
	}
}
